# 登录验证装饰器
from functools import wraps

from flask import g, make_response, jsonify, request
from flask_jwt_extended import verify_jwt_in_request, get_jwt_identity

from apps.settingApi.api.users.getPermission import userUrlPermission
from apps.settingApi.api.users.model import onLineModel
from apps.settingApi.utils import verifyDateOverdue

# 登录验证装饰器
def login_required():
    def wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            # 校验token
            try:
                verify_jwt_in_request()
            except:
                return make_response(jsonify({'msg': '登录过期'}), 400)
            # 解析获取token信息
            current_user = get_jwt_identity()
            # 全局存储用户信息
            g.user_id = current_user['user_id']
            g.username = current_user['username']
            g.code = current_user['code']
            g.user_type = current_user['user_type']
            g.lessees_id = current_user['lessees_id']
            g.lessees_type = current_user['lessees_type']
            g.lessees_name = current_user['lessees_name']
            g.lessees_end_time = current_user['lessees_end_time']
            g.end_time = current_user['end_time']
            online_id = request.headers.get('Onlineid','')
            exitonline = request.headers.get('Exitonline','')
            g.online_id = online_id
            # 判断账套是否过期
            verify_l = verifyDateOverdue(current_user['lessees_end_time'])
            if verify_l == True:
                return make_response(jsonify({'msg': '系统过期请联系管理员'}), 400)

            # 判断账号是否过期
            verify_u = verifyDateOverdue(current_user['end_time'])
            if verify_u == True:
                return make_response(jsonify({'msg': '账号过期请联系管理员'}), 400)
            endpoint = request.endpoint
            method = request.method.lower()

            # 校验用户是否下线
            if endpoint+'-'+method not in ('logoutlist-get','logoutlist-post'):
                if exitonline == True:
                    _,err = onLineModel.getPkData(online_id)
                    if err!=None:
                        return make_response(jsonify({'msg': '用户在其他设备登录或者已下线请重新登录'}), 400)

            # 判断权限
            if g.user_type!=1:
                p = userUrlPermission(current_user['user_id'],current_user['lessees_id'],endpoint+'-'+method)
                if p != True:
                    return make_response(jsonify({'msg': '对不起，您权限不足'}), 202)
            return fn(*args, **kwargs)
        return decorator
    return wrapper

# 经销商模块装饰器
# def decorator_dealer(func):
#     def wrapper(*args, **kwargs):
#         if g.lessees_type!=1:
#             return make_response(jsonify({'msg': '非经销商用户'}), 400)
#         return func(*args, **kwargs)
#     return wrapper
def decorator_dealer():
    def wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            if g.lessees_type != 1:
                return make_response(jsonify({'msg': '非经销商用户禁止访问'}), 400)
            return fn(*args, **kwargs)
        return decorator
    return wrapper